import { defineStore } from 'pinia'
import { getStorage, setStorage } from '@/utils/localstorage'
import { TOKEN } from '@/constants/constants'
import { getCurrentUser, loginByUserPassword } from '@/service/user'
import { UserInfo, UserPassword } from '#/comm/user'
import { getUserInfo } from '@/mock/user'

const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null as UserInfo | null,
  }),
  getters: {
    // 获取用户机构
    getUserOrgList(state) {
      return state.userInfo?.orgList || []
    },
    // 获取应用列表
    getUserApplicationList(state) {
      return state.userInfo?.applicationList || []
    },
    // 完整用户信息
    getUserInfo(state) {
      return state.userInfo
    },
  },
  actions: {
    async loginCloud(param: UserPassword) {
      if (!this.userInfo) {
        // let token: any = getStorage(TOKEN)
        // if (!token) {
        // token = await loginByUserPassword(param)
        // }
        // if (token) {
        //   setStorage(TOKEN, token.accessToken ? token.accessToken : token)
        // }
        setStorage(TOKEN, 'tttoookkkeeennn')
      }
    },
    async getCurrentUser() {
      console.log('获取个人信息')
      if (!this.userInfo) {
        const userInfo = getUserInfo()
        // const userInfo = await getCurrentUser()
        if (userInfo) {
          this.userInfo = userInfo
        }
      }
    },
  },
})
export default useUserStore
